<?php

namespace App\Services\User;

use App\Exceptions\ApiExceptions;
use App\Models\UserEducationModel;
use App\Models\UserWorkExperienceModel;
use App\Services\AbstractBaseService;

class WorkExperienceService extends AbstractBaseService
{

    /**
     * @param int $uid
     * @param int $work_experience_id
     * @param string $company_name 公司名称
     * @param string $start_date 开始时间
     * @param string $end_date 结束时间
     * @param string $job_description 工作描述
     * @return void
     * @throws ApiExceptions
     */
    public function update(int $uid,int $work_experience_id, string $company_name, string $start_date, string $end_date, string $job_description)
    {
        $res = UserWorkExperienceModel::query()->where('id', $work_experience_id)->where('user_id', $uid)->update(
            [
                'company_name' => $company_name,
                'start_date' => $start_date,
                'end_date' => $end_date,
                'job_description' => $job_description,
            ]
        );
        if (!$res) {
            throw new ApiExceptions('更新失败');
        }
    }
    
    /**
     * 添加工作经历
     * @param int $uid
     * @param string $company_name 公司名称
     * @param string $start_date 开始时间
     * @param string $end_date 结束时间
     * @param string $job_description 工作描述
     * @return void
     * @throws ApiExceptions
     */
    public function create(int $uid, string $company_name, string $start_date, string $end_date, string $job_description)
    {
        $flag = UserWorkExperienceModel::query()->where("user_id",$uid)->getModels();
        if(count($flag) > 10){
            throw new ApiExceptions('添加失败,最多添加10条');
        }
        $res = UserWorkExperienceModel::query()->create(
            [
                'user_id' => $uid,
                'company_name' => $company_name,
                'start_date' => $start_date,
                'end_date' => $end_date,
                'job_description' => $job_description,
            ]
        );
        if (!$res) {
            throw new ApiExceptions('添加失败');
        }
    }

    /**
     * 删除工作经历
     * @param int $uid
     * @param int $work_experience_id 工作经历id
     * @return void
     * @throws ApiExceptions
     */
     public function delete(int $work_experience_id,int $uid)
     {
         $res = UserWorkExperienceModel::query()->where('user_id', $uid)->where('id', $work_experience_id)->delete();
         if (!$res) {
             throw new ApiExceptions('删除失败');
         }
     }
}